Completed
Branch refactoring (45a804)
by Johan
01:19
created

canvas.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 5
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
nop 3
dl 5
loc 5
rs 9.4285
1
import merge from "../help/merge.js";
2
import {getEncodingHeight, getBarcodePadding} from "./shared.js";
3
4 View Code Duplication
class CanvasRenderer{
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5
	constructor(canvas, encodings, options){
6
		this.canvas = canvas;
7
		this.encodings = encodings;
8
		this.options = options;
9
	}
10
11
	render(){
12
		// Abort if the browser does not support HTML5 canvas
13
		if (!this.canvas.getContext) {
14
			throw new Error('The browser does not support canvas.');
15
		}
16
17
		this.prepareCanvas();
18
		for(let i = 0; i < this.encodings.length; i++){
19
			var encodingOptions = merge(this.options, this.encodings[i].options);
20
21
			this.drawCanvasBarcode(encodingOptions, this.encodings[i]);
22
			this.drawCanvasText(encodingOptions, this.encodings[i]);
23
24
			this.moveCanvasDrawing(this.encodings[i]);
25
		}
26
27
		this.restoreCanvas();
28
	}
29
30
	prepareCanvas(){
31
		// Get the canvas context
32
		var ctx = this.canvas.getContext("2d");
33
34
		ctx.save();
35
36
		// Calculate total width
37
		var totalWidth = 0;
38
		var maxHeight = 0;
39
		for(let i = 0; i < this.encodings.length; i++){
40
			var options = merge(this.options, this.encodings[i].options);
41
			var encoding = this.encodings[i];
42
43
			// Set font
44
			ctx.font = options.fontOptions + " " + options.fontSize + "px " + options.font;
45
46
			// Calculate the width of the encoding
47
			var textWidth = ctx.measureText(encoding.text).width;
48
			var barcodeWidth = encoding.data.length * options.width;
49
			encoding.width = Math.ceil(Math.max(textWidth, barcodeWidth));
50
51
			// Calculate the height of the encoding
52
			var height = getEncodingHeight(encoding, options);
53
54
			encoding.barcodePadding = getBarcodePadding(textWidth, barcodeWidth, options);
55
56
			if(height > maxHeight){
57
				maxHeight = height;
58
			}
59
60
			totalWidth += encoding.width;
61
		}
62
63
		this.canvas.width = totalWidth + this.options.marginLeft + this.options.marginRight;
64
65
		this.canvas.height = maxHeight;
66
67
		// Paint the canvas
68
		ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
69
		if(this.options.background){
70
			ctx.fillStyle = this.options.background;
71
			ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
72
		}
73
74
		ctx.translate(this.options.marginLeft, 0);
75
	}
76
77
	drawCanvasBarcode(options, encoding){
78
		// Get the canvas context
79
		var ctx = this.canvas.getContext("2d");
80
81
		var binary = encoding.data;
82
83
		// Creates the barcode out of the encoded binary
84
		var yFrom;
85
		if(options.textPosition == "top"){
86
			yFrom = options.marginTop + options.fontSize + options.textMargin;
87
		}
88
		else{
89
			yFrom = options.marginTop;
90
		}
91
92
		ctx.fillStyle = options.lineColor;
93
94
		for(var b = 0; b < binary.length; b++){
95
			var x = b * options.width + encoding.barcodePadding;
96
97
			if(binary[b] === "1"){
98
				ctx.fillRect(x, yFrom, options.width, options.height);
99
			}
100
			else if(binary[b]){
101
				ctx.fillRect(x, yFrom, options.width, options.height * binary[b]);
102
			}
103
		}
104
	}
105
106
	drawCanvasText(options, encoding){
107
		// Get the canvas context
108
		var ctx = this.canvas.getContext("2d");
109
110
		var font = options.fontOptions + " " + options.fontSize + "px " + options.font;
111
112
		// Draw the text if displayValue is set
113
		if(options.displayValue){
114
			var x, y;
115
116
			if(options.textPosition == "top"){
117
				y = options.marginTop + options.fontSize - options.textMargin;
118
			}
119
			else{
120
				y = options.height + options.textMargin + options.marginTop + options.fontSize;
121
			}
122
123
			ctx.font = font;
124
125
			// Draw the text in the correct X depending on the textAlign option
126
			if(options.textAlign == "left" || encoding.barcodePadding > 0){
127
				x = 0;
128
				ctx.textAlign = 'left';
129
			}
130
			else if(options.textAlign == "right"){
131
				x = encoding.width - 1;
132
				ctx.textAlign = 'right';
133
			}
134
			// In all other cases, center the text
135
			else{
136
				x = encoding.width / 2;
137
				ctx.textAlign = 'center';
138
			}
139
140
			ctx.fillText(encoding.text, x, y);
141
		}
142
	}
143
144
145
146
	moveCanvasDrawing(encoding){
147
		var ctx = this.canvas.getContext("2d");
148
149
		ctx.translate(encoding.width, 0);
150
	}
151
152
	restoreCanvas(){
153
		// Get the canvas context
154
		var ctx = this.canvas.getContext("2d");
155
156
		ctx.restore();
157
	}
158
}
159
160
export default CanvasRenderer;
161